package com.shallwead.bna.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.shallwead.bna.BnA;
import com.shallwead.bna.R;
import com.shallwead.bna.adapter.AdBackListAdapter;
import com.shallwead.bna.object.AdBack;
import com.shallwead.bna.util.JSON;
import com.shallwead.bna.util.image.ImageDownloader;

public class AdBackDialog extends Dialog implements
		android.view.View.OnClickListener {

	Context mContext;
	Activity mActivity;
	ArrayList<AdBack> adBackList;
	AdBackListAdapter adBackListAdapter;

	public AdBackDialog(Context context) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		this.mContext = context;
		this.mActivity = (Activity) context;

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_adback);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));
	}

	public AdBackDialog(Context context, ArrayList<AdBack> adBackList) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		this.mContext = context;
		this.mActivity = (Activity) context;
		this.adBackList = adBackList;

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_adback);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setLayout();

		new GetBackAdAsync(mContext).execute();
	}

	public GridView adback_gridview;
	public ImageView adback_first_icon;
	public TextView adback_first_title;

	public void setLayout() {

		adback_gridview = (GridView) findViewById(R.id.adback_gridview);
		adback_first_icon = (ImageView) findViewById(R.id.adback_first_icon);
		adback_first_title = (TextView) findViewById(R.id.adback_first_title);

		(findViewById(R.id.adback_cancel)).setOnClickListener(this);
		(findViewById(R.id.adback_finish)).setOnClickListener(this);
		(findViewById(R.id.adback_first_layout)).setOnClickListener(this);

	}

	AdBack first_adback;

	public void Init() {

		first_adback = adBackList.get(0);
		adBackList.remove(0);

		BnA.imgDownloader.download(first_adback.getImageUrl(), adback_first_icon);
		adback_first_title.setText(first_adback.getName());
		adBackListAdapter = new AdBackListAdapter(mContext, adBackList);
		adback_gridview.setAdapter(adBackListAdapter);

	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.adback_cancel) {
			cancel();
		} else if (id == R.id.adback_finish) {
			mActivity.finish();
		} else if (id == R.id.adback_first_layout) {
			BnA.goAd(mContext, first_adback);
		}
	}

	class GetBackAdAsync extends AsyncTask<String, String, Boolean> {

		Context mContext;

		public GetBackAdAsync(Context mContext) {
			this.mContext = mContext;
		}

		@Override
		protected Boolean doInBackground(String... params) {
			try {
				adBackList = JSON.getBackAdList();
				return true;
			} catch (Exception e) {
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (adBackList.size() == 0) {
				dismiss();
				((Activity) mContext).finish();
			}
			if (result && adBackList.size() > 0) {
				Init();
			}
		}

	}

}
